import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.*;
import org.newdawn.slick.tiled.TiledMap;

public class OctopusMain extends BasicGame {

	private TiledMap mapGen;
	private EntityPlayer bunny;
	private EntityCarrot carrot;

	public OctopusMain() {
		super("OrangeOctopus");
	}

	Map map;
	AStar pathFind;
	SpeechHandler sh;
	int action;
	int volume;
	

	public void init(GameContainer gc) throws SlickException {
		//mapGen = new TiledMap("content/tiles1.tmx");
		map = new Map("content/testmaze.tmx");

		pathFind = new AStar();
		
		sh = new SpeechHandler();
		Runnable r = new Runnable() {
			public void run() {
				while(true) {
					action = sh.getAction();
					volume = sh.getVolume();
				}
			}
		};
		Thread th = new Thread(r);
		th.start();
		
		bunny = new EntityPlayer(4, 5);
		carrot = new EntityCarrot(13,10);

	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		Display.sync(60);
		//System.out.println(mapGen.getTileId(8, 7, 0));
		//System.out.println(mapGen.getObjectProperty(0, 34));
		int dir = pollInput();

		//int d = pathFind.getAction(bunny.posX, bunny.posY, 11, 10);
		/*
		if (carrot.gotIt == true) {
			// after a delay, show play screen, move carrot and player back to initial position
			
			try {
				Thread.sleep(4000);
				bunny.setPos(6, 5);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			 
			 
		}
		*/
		
		bunny.move(action);
		if(dir != BunnyAction.NULL) {
			bunny.move(dir, volume, 11, 10);
		}
		
		if(bunny.posX == 13 && bunny.posY == 10) {
			carrot.gotIt = true;
		} else {
			carrot.gotIt = false;
		}
		
		carrot.update();
		bunny.update();
	}

	public void render(GameContainer gc, Graphics g) throws SlickException {
		//mapGen.render(0,0); //needs to be changed
		map.render();
		bunny.render();
		carrot.render();
		g.drawString("000000000    SUPERd: BUN GAME", 73, 10);
	}

	public static void main(String[] args) throws SlickException {
		AppGameContainer app = 
				new AppGameContainer(new OctopusMain());

		app.setDisplayMode(800, 600, false);
		app.start();
	}

	public int pollInput() {
		int action = BunnyAction.NULL;

		if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
			action = BunnyAction.LEFT;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
			action = BunnyAction.RIGHT;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			action = BunnyAction.UP;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
			action = BunnyAction.DOWN;
		}

		while(Keyboard.next()) {

			if (Keyboard.getEventKey() == Keyboard.KEY_UP) {
				System.out.println("UP");
				action = BunnyAction.UP;
			}

			if (Keyboard.getEventKey() == Keyboard.KEY_DOWN) {
				System.out.println("DOWN");
				action = BunnyAction.DOWN;
			}

			if (Keyboard.getEventKey() == Keyboard.KEY_LEFT) {
				System.out.println("LEFT");
				action = BunnyAction.LEFT;

			}

			if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT) {
				System.out.println("RIGHT");
				action = BunnyAction.RIGHT;
			}
		}
		return action;
	}
}